<template>
  <div class="app-container">
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- <el-select
          v-model="query.countryId"
          size="small"
          style="width: 180px"
          class="filter-item"
          clearable
          placeholder="请选择国家/地区"
        >
          <el-option
            v-for="(item, index) in country"
            :key="index"
            :label="item.countryName"
            :value="item.countryCode"
          />
        </el-select> -->
        <!-- <el-date-picker
          v-model="params.date"
          clearable
          type="datetimerange"
          size="small"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="filter-item"
          range-separator="~"
          style="width: 380px"
          start-placeholder="请选择开始时间"
          end-placeholder="请选择到期时间"
        /> -->
        <!-- <rrOperation :crud="crud" /> -->
      </div>
      <crudOperation :permission="permission" />
    </div>
    <el-drawer
      :visible.sync="crud.status.cu > 0"
      :before-close="crud.cancelCU"
      direction="rtl"
      :append-to-body="false"
      custom-class="common-drawer"
      size="600px"
      :show-close="false"
    >
      <div slot="title" class="drawer-title">
        <span>{{ crud.status.title }}</span>
        <span class="el-icon-close" @click="crud.cancelCU" />
      </div>
      <div class="drawer-content">
        <el-scrollbar
          :native="false"
          :noresize="false"
          class="dark fix bannerform"
        >
          <el-form
            ref="form"
            :inline="true"
            :model="form"
            :rules="rules"
            size="small"
            label-width="120px"
          >
            <el-form-item label="广告图" prop="bannerUrl">
              <el-upload
                class="avatar-uploader"
                action
                :auto-upload="false"
                :show-file-list="false"
                :on-change="changeFile"
              >
                <img
                  v-if="form.bannerUrl"
                  :src="form.bannerUrl"
                  class="avatar"
                >
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
              <p class="warning-text" style="font-size: 12px">
                推荐尺寸：720*300
              </p>
            </el-form-item>
            <!-- <el-form-item label="国家/地区" prop="countryId">
              <el-select
                v-model="form.countryId"
                size="small"
                style="width: 360px"
                class="filter-item"
                placeholder="请选择状态"
              >
                <el-option
                  v-for="(item, index) in country"
                  :key="index"
                  :label="item.countryName"
                  :value="item.countryCode"
                />
              </el-select>
            </el-form-item> -->
            <!-- <el-form-item label="平台" prop="platform">
              <el-select
                v-model="form.platform"
                size="small"
                style="width: 360px"
                class="filter-item"
                placeholder="请选择状态"
              >
                <el-option
                  v-for="(item) in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="展示位置" prop="type">
              <el-select
                v-model="form.type"
                size="small"
                style="width: 360px"
                class="filter-item"
                placeholder="请选择状态"
              >
                <el-option
                  v-for="(item) in options1"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item> -->
            <el-form-item label="广告链接" prop="link">
              <el-input v-model="form.link" style="width: 360px" />
            </el-form-item>
            <el-form-item label="广告权重" prop="weight">
              <el-input v-model="form.weight" style="width: 360px" />
              <p class="warning-text" style="font-size: 12px">
                权重值越小越靠前
              </p>
            </el-form-item>
            <!-- <el-form-item label="显示时间" prop="date">
              <template>
                <el-date-picker
                  v-model="form.startDate"
                  type="datetime"
                  style="display: block;margin-bottom: 12px"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="请选择开始时间"
                ></el-date-picker>
                <el-date-picker
                  v-model="form.createTime"
                  clearable
                  type="datetimerange"
                  size="small"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  class="filter-item"
                  range-separator="~"
                  style="width: 360px"
                  start-placeholder="请选择开始时间"
                  end-placeholder="请选择到期时间"
                />
              </template>
            </el-form-item> -->
          </el-form>
        </el-scrollbar>
        <div class="fix-bottom">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button
          >
        </div>
      </div>
    </el-drawer>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column label="编号" type="index" width="50" />
      <el-table-column
        v-if="columns.visible('bannerUrl')"
        :show-overflow-tooltip="true"
        prop="bannerUrl"
        label="广告图"
      >
        <template slot-scope="scope">
          <img
            v-if="scope.row.bannerUrl"
            :src="scope.row.bannerUrl"
            style="height: 40px"
            alt
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="columns.visible('link')"
        :show-overflow-tooltip="true"
        prop="link"
        label="链接"
      />
      <el-table-column
        v-if="columns.visible('weight')"
        :show-overflow-tooltip="true"
        prop="weight"
        label="权重"
      />
      <!-- <el-table-column
        v-if="columns.visible('countryName')"
        prop="countryName"
        label="国家/地区"
      >
      </el-table-column> -->
      <el-table-column
        v-if="columns.visible('createTime')"
        :show-overflow-tooltip="true"
        prop="createTime"
        label="创建时间"
      />

      <el-table-column label="操作" width="150" fixed="right">
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            msg="确定要删除该广告图？"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import CRUD, { presenter, header, crud, form } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import bannerMethod from '../../../api/banner'
import { regexp } from '../../../utils/regexp'
import { parseTime } from '../../../utils'
import { uploadFile } from '@/api/common/index'
import { countryList } from '@/api/banner'

// crud交由presenter持有
const defaultCrud = CRUD({
  url: '/banner/list',
  title: '广告图',
  crudMethod: { ...bannerMethod }
})

const defaultForm = {
  name: null,
  startDate: null,
  endDate: null,
  sortNum: null,
  imageAddress: '',
  link: null,
  note: null,
  countryId: null
  // type: 1,
  // platform: null
}
export default {
  name: 'Banner',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), crud(), form(defaultForm)],
  data() {
    return {
      params: {},
      permission: {
        add: ['admin', 'banner:add'],
        edit: ['admin', 'banner:edit'],
        del: ['admin', 'banner:del']
      },
      country: [],
      options: [
        {
          value: 'pc',
          label: 'PC'
        },
        {
          value: 'android',
          label: 'Android'
        },
        {
          value: 'ios',
          label: 'IOS'
        }
      ],
      options1: [
        {
          value: 1,
          label: '首页'
        }
        // {
        //   value: 2,
        //   label: '导航页'
        // }
      ],
      rules: {
        date: [
          { required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        // platform: [
        //   { required: true, message: '请选择平台', trigger: 'change' }
        // ],
        // type: [
        //   { required: true, message: '请选择位置', trigger: 'change' }
        // ],
        link: [{ required: true, message: '请输入广告链接', trigger: 'blur' }],
        bannerUrl: [
          { required: true, message: '请上传广告图', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    countryList().then((res) => {
      console.log(res)
      if (res.code == 0) {
        this.country = res.data
      } else {
        this.$message.error(res.message)
      }
    })
  },
  methods: {
    [CRUD.HOOK.beforeReset]() {
      this.params = {}
    },
    [CRUD.HOOK.beforeRefresh](crud) {
      const { date } = this.params
      console.log(date)
      if (date) {
        const [startDate, endDate] = date
        crud.query.startDate = startDate
        crud.query.endDate = endDate
      } else {
        crud.query.startDate = ''
        crud.query.endDate = ''
      }
    },

    // 获取值
    subshutype3() {
      this.crud.form.desc = this.phoneEditor.txt.html()
    },

    [CRUD.HOOK.beforeSubmit](crud) {
      // crud.form.recommandStatus = crud.form.recommandStatus.join(",");
      // crud.form.recommandStatus = crud.form.recommandStatus.join(",");
      // if (crud.form.nature.length === 0) {
      //   delete crud.form.nature;
      // }
    },
    [CRUD.HOOK.afterToCU](crud) {},
    [CRUD.HOOK.beforeToAdd](crud) {
      console.log(crud.form, '111')
      this.$set(crud.form, 'bannerUrl', null)
      this.$set(crud.form, 'countryId', null)
      this.$set(crud.form, 'createTime', null)
      this.$set(crud.form, 'endDate', null)
      this.$set(crud.form, 'id', null)
      this.$set(crud.form, 'imageAddress', null)
      this.$set(crud.form, 'link', null)
      this.$set(crud.form, 'name', null)
      this.$set(crud.form, 'note', null)
      this.$set(crud.form, 'platform', null)
      this.$set(crud.form, 'sortNum', null)
      this.$set(crud.form, 'startDate', null)
      this.$set(crud.form, 'status', null)
      this.$set(crud.form, 'weight', null)
    },

    [CRUD.HOOK.beforeToEdit](crud) {
      console.log(crud.form)
      const str = parseTime(crud.form.startDate)
      const strend = parseTime(crud.form.endDate)

      this.$set(crud.form, 'startDate', str)
      this.$set(crud.form, 'endDate', strend)
    },
    changeFile(fileRaw) {
      const file = fileRaw.raw
      const isJPG = file.type.indexOf('image') >= 0
      const isLt2M = file.size / 1024 / 1024 < 2
      // if (!isJPG) {
      //   this.$message.error('上传图片只能是图片格式!')
      //   return
      // }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
        return
      }
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 0)
      this.form.imgFile = file
      uploadFile(formData).then((res) => {
        console.log(res.code)
        if (res.code == 0) {
          this.$set(this.form, 'bannerUrl', res.data.banner)
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.bannerform {
  .avatar-uploader .el-upload {
    width: 360px !important;
    height: 150px !important;
    border: 1px dashed #d9d9d9 !important;
    border-radius: 6px !important;
    cursor: pointer !important;
    position: relative !important;
    overflow: hidden !important;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff !important;
  }

  .avatar-uploader-icon {
    font-size: 18px !important;
    color: #8c939d !important;
    width: 360px !important;
    height: 150px !important;
    line-height: 150px !important;
    text-align: center !important;
  }

  .avatar {
    width: 360px !important;
    height: 150px !important;
    display: block !important;
  }
}
</style>
